package cn.truistic.enmicromsg.wx.wx_keysendmessage;

import android.accessibilityservice.AccessibilityService;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import cn.truistic.enmicromsg.app.EnApplication;
import cn.truistic.enmicromsg.common.util.IDConstant;
import cn.truistic.enmicromsg.common.util.PreferenceHelper;
import cn.truistic.enmicromsg.common.util.RootUtil;
import cn.truistic.enmicromsg.common.util.WxHeple;

/**
 * Created by admin on 2018/6/19.
 * SJ： 16点59分
 * name: ts
 * 用途：一键发送消息
 */

public class KeySendMessageHeper extends WxHeple {
    private boolean isnotsend;
    private int mContactlistCurrent = 0;
    /**
     * 一键发消息默认内容
     */
    private String AKeyContent = "hello!!!";

    public KeySendMessageHeper( EnApplication mContext, AccessibilityService mAccessibilityService ) {
        super(mContext, mAccessibilityService);
    }

    //进行自动化 ----附近的人打招呼
    @Override
    public void isAutomationNearby(AccessibilityEvent event, int eventType ) {
        if (mEnApplication.isAllowAKeySendMessage()){
            Log.i("TAG", isnotsend + "");
            if (!isnotsend) {
                isnotsend = false;
                sleepTime(1000);
                while (!isFindText("通讯录")) {
                    sleepTime(500);
                }
                if (isFindId(IDConstant.MAIN_BUTTON) && isFindText("通讯录")) {
                    WXfindIdTextAndClick(IDConstant.MAIN_BUTTON, "通讯录", 1);
                    //findTextAndClick("通讯录", 0);
                }
            } else {
                isnotsend = false;
            }
            Log.i("TAG", mContactlistCurrent + ":" + isFindIdlistnum(IDConstant.WX_CONTACT_LIST));
            if (mContactlistCurrent == isFindIdlistnum(IDConstant.WX_CONTACT_LIST)) {
                Log.i("TAG", isFindIdlistnum(IDConstant.WX_CONTACT_LIST) + "");
                if ( isFindId( IDConstant.WX_CONTACT_agp ) ) {
                    mEnApplication.setAllowAKeySendMessage(false);
                    mEnApplication.setAKeySendMessage(false);
                    mContactlistCurrent = 0 ;
                    execShellCmd("input keyevent 4");
                    Toast.makeText(mEnApplication, "服务已完结", Toast.LENGTH_SHORT).show();
                } else {
                    sleepTime(1500);
                    RootUtil.execSlipCmd("input swipe 400 1100 400 400");
                    isnotsend = true;
                    sleepTime(5000);
                    Log.i("TAG", mContactlistCurrent + isFindIdlistnum(IDConstant.WX_CONTACT_LIST) + "");
                    if (isFindId(IDConstant.WX_CONTACT_agp) && isFindIdlistnum(IDConstant.WX_CONTACT_LIST) < 8 && mContactlistCurrent < 8) {
                        mContactlistCurrent = isFindIdlistnum(IDConstant.WX_CONTACT_LIST) - mContactlistCurrent;
                    } else {
                        mContactlistCurrent = 0;
                    }
                    Log.i("TAG", mEnApplication.isAllowAKeySendMessage() + "1");
                    findIdAndClick(IDConstant.WX_CONTACT_LIST, mContactlistCurrent);
                    mEnApplication.setAllowAKeySendMessage(false);
                    mContactlistCurrent++;

                }

            } else {
                sleepTime(1500);
                Log.i("TAG", mEnApplication.isAllowAKeySendMessage() + "1");
                while (!isFindId(IDConstant.WX_CONTACT_LIST, mContactlistCurrent)) {
                    sleepTime(500);
                }
                findIdAndClick( IDConstant.WX_CONTACT_LIST, mContactlistCurrent);
                mEnApplication.setAllowAKeySendMessage( false );
                mContactlistCurrent++ ;
            }
        }/**
         * 详细资料页面
         */
        else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.ContactInfoUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            while (!isFindText("发消息")) {
                sleepTime(500);
            }
            if (isFindText("微信团队") || isFindText("文件传输助手")) {
                sleepTime(1000);
                isnotsend = true;
                mEnApplication.setAllowAKeySendMessage( true );
                findIdAndClick( IDConstant.WX_BACK , 0 );
            } else {
                if ( isFindText("发消息") && isFindId(IDConstant.WX_SEND_MESSAGE )) {
                     sleepTime(1000);
                     WXfindIdTextAndClick(IDConstant.WX_SEND_MESSAGE, "发消息", 0 );
                    //findTextAndClick("发消息", 0);
                }
            }
        }
        /**
         * 聊天界面
         */
        else if (event.getClassName().equals("com.tencent.mm.ui.chatting.ChattingUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            while (!isFindId(IDConstant.WX_AKEYCHAT_EDIT)) {
                sleepTime(500);
            }
            if (isFindId(IDConstant.WX_AKEYCHAT_EDIT)) {
                sleepTime(1000);
                findIdAndClick(IDConstant.WX_AKEYCHAT_EDIT, 0);
                String helloStr = PreferenceHelper.getString( "OneKeyDialogText", "");
                if (TextUtils.isEmpty(helloStr)){
                    helloStr = AKeyContent;
                }
                sleepTime(1000);
                inputHello(helloStr);
                sleepTime(1500);
                findIdAndClick(IDConstant.WX_AKEYCHAT_SEND, 0);
                sleepTime(1000);
                //返回
                findIdAndClick(IDConstant.WX_CHATTING_BACK, 0);
                mEnApplication.setAllowAKeySendMessage( true ) ;
                Log.i("TAG", mEnApplication.isAllowAKeySendMessage() + "2");
            }
        }
    }
}
